﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;
using SchoolBusiness;

namespace SchoolAdministration
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private readonly BLRoutine _blroutine = new BLRoutine();
        private Subject sub = new Subject();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Grade> grade = _blroutine.GetGrade();
                ClassDropdown.DataSource = grade;
                ClassDropdown.DataTextField = "ClassName";
                ClassDropdown.DataValueField = "ClassId";
                ClassDropdown.DataBind();
                TxtSubject.Text = "";
            }
            int SubjectId =Convert.ToInt32(Request.QueryString["SubjectId"]);
            LoadEditSubjectInfo(SubjectId);

        }
        public void LoadEditSubjectInfo(int subjectid) {
            TXTSubjectId.Text = subjectid.ToString();
            TXTSubjectId.Enabled = false;
            sub = _blroutine.GetSubjectById(subjectid);
            TxtSubject.Attributes.Add("Value", sub.SubjectName);
            TxtSubjectCode.Attributes.Add("Value",sub.SubjectCode);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            sub.SubjectName = TxtSubject.Text;
            sub.SubjectCode = TxtSubjectCode.Text;
            sub.ClassGrade.ClassId = Int32.Parse(ClassDropdown.Text);
            int SubjectId = Convert.ToInt32(Request.QueryString["SubjectId"]);
            sub.SubjectId = SubjectId;
            int result = _blroutine.UpdateSubjectById(sub);
            if (result > 0)
            {
                Response.Redirect("ViewSubject.aspx");
                Helper.Alert.Show("Successfully Edited subject");
                
            }



        }
    }
}